package vn.edu.saigontech.ncms.web.system;

import java.util.Date;

import vn.edu.saigontech.ncms.model.system.DayOff;
import vn.edu.saigontech.ncms.service.DayOffService;
import vn.edu.saigontech.ncms.util.Checker;
import vn.edu.saigontech.ncms.util.DateUtil;
import vn.edu.saigontech.ncms.web.BaseAction;

@SuppressWarnings("serial")
public class DayOffAction extends BaseAction {

	private Long id;
	private boolean yearlyRepeat;
	private String date;
	private String endDate;
	private String comment;

	public String list() {
		if (!Checker.isNull(this.id)) {
			REQUEST().setAttribute("dayOff", DayOffService.INSTANCE.findById(this.id));
		}
		REQUEST().setAttribute("dayOffs", DayOffService.INSTANCE.findAll());
		return SUCCESS;
	}

	public String edit() {
		Date dateDate = DateUtil.fromString(this.date, this.i18n("format.date"));
		Date endDate = DateUtil.fromString(this.endDate, this.i18n("format.date"));
		if (!Checker.isNull(dateDate)) {
			DayOff dayOff = DayOffService.INSTANCE.findByDate(dateDate);
			if (Checker.isNull(dayOff)) {
				dayOff = new DayOff();
				dayOff.setDate(dateDate);
			}
			dayOff.setEndDate(endDate);
			dayOff.setComment(this.comment);
			dayOff.setYearlyRepeat(yearlyRepeat);
			DayOffService.INSTANCE.persist(dayOff);
			this.id = dayOff.getId();
		}
		return SUCCESS;
	}

	public String delete() {
		DayOffService.INSTANCE.deleteById(this.id);
		return SUCCESS;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getDate() {
		return date;
	}

	public void setDate(String date) {
		this.date = date;
	}

	public String getComment() {
		return comment;
	}

	public void setComment(String comment) {
		this.comment = comment;
	}

	public String getEndDate() {
		return endDate;
	}

	public void setEndDate(String endDate) {
		this.endDate = endDate;
	}

	public boolean isYearlyRepeat() {
		return yearlyRepeat;
	}

	public void setYearlyRepeat(boolean yearlyRepeat) {
		this.yearlyRepeat = yearlyRepeat;
	}
}
